package com.imooc.dmtest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import com.imooc.entity.Book;

public class JDOMTest {

	public static void main(String[] args) {
		new JDOMTest().createXML();
	}

	private void parseXML() {
		// 进行对books.xml文件的JDOM解析
		// 准备工作
		// 1.创建一个SAXBuilder的对象
		SAXBuilder saxBuilder = new SAXBuilder();
		InputStream in;
		List<Book> booksList = new ArrayList<Book>();
		try {
			// 2.创建一个输入流，将xml文件加载到输入流中
			in = new FileInputStream("src/main/resources/xmlfile/books.xml");
			// 3.通过saxBuilder的build方法，将输入流加载到saxBuilder中
			Document document = saxBuilder.build(in);
			// 通过document对象获取xml文件的根节点
			Element rootElement = document.getRootElement();
			// 获取根节点下的子节点的List集合
			List<Element> bookList = rootElement.getChildren();
			// 继续进行解析
			for (Element book : bookList) {
				Book bookEntity = new Book();
				System.out.println("开始解析第" + (bookList.indexOf(book) + 1) + "本书==========");
				// 解析book的属性
				List<Attribute> attrList = book.getAttributes();
				for (Attribute attribute : attrList) {
					// 获取属性名
					String attrName = attribute.getName();
					// 获取属性值
					String attrValue = attribute.getValue();
					System.out.println("属性名：" + attrName + " -----属性值：" + attrValue);
					if (attrName.equals("id")) {
						bookEntity.setId(attrValue);
					}
				}
				// 对book节点的子节点的节点名以及节点值的遍历
				List<Element> bookChilds = book.getChildren();
				for (Element child : bookChilds) {
					System.out.println("节点名：" + child.getName() + "------节点值：" + child.getValue());
					if (child.getName().equals("name")) {
						bookEntity.setName(child.getValue());
					}
					if (child.getName().equals("author")) {
						bookEntity.setAuthor(child.getValue());
					}
					if (child.getName().equals("year")) {
						bookEntity.setYear(child.getValue());
					}
					if (child.getName().equals("price")) {
						bookEntity.setPrice(child.getValue());
					}
					if (child.getName().equals("language")) {
						bookEntity.setLanguage(child.getValue());
					}

				}
				System.out.println("==========结束解析第" + (bookList.indexOf(book) + 1) + "本书==========");
				booksList.add(bookEntity);
				bookEntity = null;
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void createXML(){
		//1.生成一个根节点
		Element rss = new Element("rss");
		//2.为节点添加属性
		rss.setAttribute("version", "2.0");
		//3.生成一个document对象
		Document document = new Document(rss);
		
		Element channel = new Element("channel");
		Element title = new Element("title");
		//不存在特殊符号，不需要转义
		//title.setText("国内最新新闻");
		//存在特殊符号，禁止转义
		title.addContent(new CDATA("国内最新新闻"));
		channel.addContent(title);
		rss.addContent(channel);
		
		Format format = Format.getCompactFormat();
		format.setIndent("");
		format.setEncoding("GBK");
		
		//4.创建XMLOutputter的对象
		XMLOutputter outputer = new XMLOutputter(format);
		
		//5.利用outputer将document对象转换成xml文档。
		try {
			outputer.output(document, new FileOutputStream(new File("rssxml.xml")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	
}




























